import logging
from typing import Optional
import typing

import PySide6.QtCore
from PySide6.QtGui import QAction, QIcon
import PySide6.QtWidgets
from PySide6.QtCore import Qt
from PySide6.QtWidgets import (QGridLayout, QHBoxLayout, QToolBox, QTreeWidget, QTreeWidgetItem,
                               QWidget, QToolButton, QSpacerItem, QSizePolicy)


class ToolWidgetBar(QWidget):
    '''
    description:
        一个通用的装有ToolButton的Widget
    args:
        parent 'QWidget' 父窗体
        f 'WindowsFlags' 窗体类型，默认为 Qt.Widget
    return:
        None
    '''
    def __init__(
        self, 
        parent: Optional[PySide6.QtWidgets.QWidget] = None, 
        f: PySide6.QtCore.Qt.WindowFlags = Qt.Widget) -> None:
        super().__init__(parent=parent, f=f)
        #####################
        self._hBoxLayout = QHBoxLayout(self)
        self._hBoxLayout.setObjectName('gridLayout')
        # self.resize()
        self._hBoxLayout.setSpacing(0)
        self._hBoxLayout.setContentsMargins(0,0,0,0)
        self.horizontalSpacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
        self._hBoxLayout.addItem(self.horizontalSpacer)
        # 1 设置类内数据
        # self._count = 0
        # self.setGeometry()
    
    def InsertToolButton(
        self, 
        action: 'QAction',
        ) -> 'bool':
        '''
        function:
            在指定的位置添加一个ToolButton，并指定该按钮按下后调用的函数
        agrs:
            name 'str' 必选 插入的button的名字
            action 'QAction' 必选 本按钮按下后所触发的动作
            icon 'QIcon' 可选 按钮所表现的图标
            index 'int' 可选 要插入的位置
            tooltip_text 'str' 鼠标悬浮文字
        return:
            bool 插入成功，返回true,插入失败返回False
        '''
        insert_index_ = self._hBoxLayout.count() - 1
        tool_button_ = QToolButton(self)
        tool_button_.setToolButtonStyle(Qt.ToolButtonIconOnly)
        tool_button_.setDefaultAction(action)
        self._hBoxLayout.insertWidget(insert_index_, tool_button_)
        return True
    
